#include "system.h"

#define I2C_TIMEOUT                      0xFF
#define BUF_SIZE                         6
uint8_t rx_buf[BUF_SIZE] = {0};

uint8_t tx_buf[BUF_SIZE] = {0x30};

/**********************************************************
函数功能：温湿度上电初始化配置
入口参数：无
出口参数：无
说	  明 
***********************************************************/ 
void flapp_t_h_init(void)
{
	//fldrv_i2c_write_byte(ENS210_REG_SYS_CTRL,0x80);  	//复位ens210
 	fldrv_i2c_write_byte(ENS210_REG_SYS_CTRL,0x00);  	//device stays in active
 	fldrv_i2c_write_byte(ENS210_REG_SENS_RUN,0x00);  	//T & H	enable  continueous模式改single模式
	fldrv_i2c_write_byte(ENS210_REG_SENS_START,0x03); //T & H measurement
}

/**********************************************************
函数功能：crc检验函数
入口参数：uint32_t val
出口参数：无
说	  明 
***********************************************************/ 
static uint32_t crc7( uint32_t val ) 
{
	uint32_t bit,pol;
	// Setup polynomial
	pol= CRC7POLY;
	// Align polynomial with data
	pol = pol << (DATA7WIDTH-CRC7WIDTH-1);
	// Loop variable (indicates which bit to test, start with highest)
	bit = DATA7MSB;
	// Make room for CRC value
	val = val << CRC7WIDTH;
	bit = bit << CRC7WIDTH;
	pol = pol << CRC7WIDTH;
	// Insert initial vector
	val |= CRC7IVEC;
	// Apply division until all bits done
	while( bit & (DATA7MASK<<CRC7WIDTH) ) {
		if( bit & val ) val ^= pol;
		bit >>= 1;
		pol >>= 1;
	}
	return val;
}

/**********************************************************
函数功能：温湿度运行任务
入口参数：无
出口参数：无
说	  明 
***********************************************************/ 
void flapp_t_h_run(void)
{
	int16_t tem_value;
	uint32_t t_val,h_val;
	uint32_t rd_data,rd_valid,rd_crc,rd_payl,rd_crc_ok;
	static uint8_t t_err = 0,h_err = 0;
	static uint16_t read_val[2] = {0};
	uint8_t read_data[6] = {0};
	uint8_t uart_get_air_quality_buf[UART4_TX_BUF] = {0x11,0x01,0x16,0xd8};	//空气质量查询指令
	
	if(flapp_get_device_type() != DEVICE_MASTER)	//只有作为主机才有网络部分通讯功能
		return;
	
	fldrv_uart_send_buf(USART4,uart_get_air_quality_buf,sizeof(uart_get_air_quality_buf));
	
	fldrv_i2c_read_byte(ENS210_REG_T_L,&read_data[0]);
	fldrv_i2c_read_byte(ENS210_REG_T_M,&read_data[1]);
	fldrv_i2c_read_byte(ENS210_REG_T_H,&read_data[2]);
	fldrv_i2c_read_byte(ENS210_REG_H_L,&read_data[3]);
	fldrv_i2c_read_byte(ENS210_REG_H_M,&read_data[4]);
	fldrv_i2c_read_byte(ENS210_REG_H_H,&read_data[5]);
	
	t_val= (read_data[2]<<16) + (read_data[1]<<8) + read_data[0];
	h_val= (read_data[5]<<16) + (read_data[4]<<8) + read_data[3];
	// Extract (and print) the fields
	rd_data = 	(t_val >> 0 ) & 0xffff;
	rd_valid = 	(t_val >> 16) & 0x1;
	rd_crc = 		(t_val >> 17) & 0x7f;
	rd_payl = 	(t_val >> 0 ) & 0x1ffff;
	
	rd_crc_ok = crc7(rd_payl) == rd_crc;
	
	if(!rd_crc_ok || !rd_valid || !rd_crc_ok)
	{
		t_err++;
		if(t_err >= ERR_TEMP_COUNT)
		{
			t_err = ERR_TEMP_COUNT;
			flapp_t_h_init();
			flapp_set_master_err(TRUE,DEVICE_ERROR_TEMP);
		}
	}
	else
	{
		t_err = FALSE;
		tem_value = ((int16_t)(rd_data - 42) >> 6) - 273;		//减0.65℃转换成-42
		if((tem_value >= -30) && (tem_value <= 70))
			read_val[0] = tem_value;	
		flapp_set_master_err(FALSE,DEVICE_ERROR_TEMP);
	}
	
	
	rd_data = 	(h_val >> 0 ) & 0xffff;
	rd_valid = 	(h_val >> 16) & 0x1;
	rd_crc = 		(h_val >> 17) & 0x7f;
	rd_payl = 	(h_val >> 0 ) & 0x1ffff;
	
	
	if(!rd_crc_ok || !rd_valid || !rd_crc_ok)
	{
		h_err++;
		if(h_err >= ERR_HUM_COUNT)
		{
			h_err = ERR_HUM_COUNT;
			flapp_t_h_init();
			flapp_set_master_err(TRUE,DEVICE_ERROR_HUM);
		}
	}
	else
	{
		h_err = FALSE;
		tem_value = (int16_t)(rd_data >> 9);
		if(tem_value < 1)		tem_value = 1;
		if(tem_value > 99)	tem_value = 99;
		read_val[1] = tem_value;
		flapp_set_master_err(FALSE,DEVICE_ERROR_HUM);
	}
		
	fldrv_i2c_write_byte(ENS210_REG_SENS_START,0x03);	//continueous模式改single模式后，每次读取完Enable T & H，
	if(t_err && h_err)
	{
		return;
	}

	xQueueSend(t_h_queue,read_val,0);
}


